'use strict';
const Service = require('egg').Service;
const { failRes, successRes } = require('../extend/helper');
const { Journal } = require('../exception/Journal');
const { Op } = require('sequelize');

function toInt (str) {
  if (typeof str === 'number') return str;
  if (!str) return str;
  return parseInt(str, 10) || 0;
}

class sResource extends Service {
  // pc
  // 查
  async pc_s_query (params) {
    console.log('params: ', params);
    let response = null;
    const { ctx } = this;
    try {
      let whereMap = {
        name: {
          [Op.like]: `%${(params?.name ?? '')}%` // 使用 Op.like 实现模糊匹配
        }
      };
      if (params?.classificate) {
        whereMap = {
          classificate: params?.classificate ?? 1,
          name: {
            [Op.like]: `%${(params?.name ?? '')}%` // 使用 Op.like 实现模糊匹配
          }
        }
      }
      const currentPage = toInt(params?.currentPage ?? 1);
      const pageSize = toInt(params?.pageSize ?? 10);
      const result = await ctx.model.Resource.findAndCountAll({
        where: whereMap,
        order: [['id', 'DESC']], // 排序
        limit: pageSize, // 每页多少条
        offset: pageSize * (currentPage - 1), // 跳过多少条
      })
      await Journal(ctx, { operatorType: '资源文件 -> 查询', code: 200 });
      response = successRes({
        records: (result?.rows ?? []),
        total: (result?.count ?? 0),
        pageSize,
        currentPage
      });
    } catch (error) {
      console.error(error, '<---- error - 资源文件查询');
      await Journal(ctx, { operatorType: '资源文件 -> 查询', code: 401 });
      response = failRes(401);
    }
    return response;
  }

  // 增
  async pc_s_create (params) {
    let response = null;
    const { ctx } = this;
    try {
      await ctx.model.Resource.create(
        params,
        { attributes: { extends: ['id', 'classificate', 'name', 'url', 'remarks'] } } // 向数据库插入特定字段
      );
      await Journal(ctx, { operatorType: '资源文件 -> 新增', code: 200 });
      response = successRes();
    } catch (error) {
      console.error(error, '<---- error - 资源文件新增');
      await Journal(ctx, { operatorType: '资源文件 -> 新增', code: 401 });
      response = failRes(401);
    }
    return response;
  }

  // 改
  async pc_s_update (params) {
    let response = null;
    try {
      const { ctx } = this;
      await ctx.model.Resource.update(
        params,
        {
          where: { id: params.id },
          attributes: { extends: ['id'] }
        }
      );
      await Journal(ctx, { operatorType: '资源文件 -> 编辑', code: 200 });
      response = successRes();
    } catch (error) {
      console.error(error, '<---- error - 资源文件编辑');
      await Journal(ctx, { operatorType: '资源文件 -> 编辑', code: 401 });
      response = failRes(401);
    }
    return response;
  }

  // 删
  async pc_s_delete (params) {
    let response = null;
    try {
      const { ctx } = this;
      await ctx.model.Resource.destroy(
        {
          where: { id: params.id },
        }
      );
      await Journal(ctx, { operatorType: '资源文件 -> 删除', code: 200 });
      response = successRes();
    } catch (error) {
      console.error(error, '<---- error - 资源文件删除');
      await Journal(ctx, { operatorType: '资源文件 -> 删除', code: 401 });
      response = failRes(401);
    }
    return response;
  }
}

module.exports = sResource;
